<?php

/**
 * 字符操作类
 *
 * @author hda
 */

namespace library\common;

class str {
    // <editor-fold defaultstate="collapsed" desc="随机N位数字或者字母 -by lsx">

    public static function randomStr($num) {
        $str = '';
        $pattern = '1234567890abcdefghijklmnopqrstuvwxyz   
               ABCDEFGHIJKLOMNOPQRSTUVWXYZ';
        for($i=0;$i<$num;$i++)
        {
            $str.= $pattern{mt_rand(0,35)};    //生成php随机数
        }
        return  $str;
    }

    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="随机N位数字 -by hda">

    public static function randomNum($num) {
        $str = '';
        for ($i = 0; $i < $num; $i++) {
            $str.=rand(0, 9);
        }
        return $str;
    }

    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="16位订单编号2016101810302081 年月日时分秒毫秒">
    public static function orderNo() {
//        list($s1, $s2) = explode(' ', microtime());
//        $second = date('YmdHis', $s2);
//        $milli = round($s1, 2) * 100;
//        return $second . $milli;
        $weimiao = str_pad(substr(gettimeofday()['usec'], -3, 3), 3, 0, STR_PAD_LEFT); //获取微秒后3位数,不足补0
        $orderNo = date('YmdHis') . $weimiao;
        return $orderNo;
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="数字前补零">
    public static function fillZeroFront($number,$max_length){
        $len_str=strlen(strval($number));
        if($len_str<$max_length){
            $num=$max_length-$len_str;
            $str= strval($number);
            for ($i = 0; $i < $num; $i++) {
                $str='0'.$str;
            }
            return $str;
        }else{
            return $number;
        }
    }
// </editor-fold>


// <editor-fold defaultstate="collapsed" desc="判断指定时间段比如（格式H:i）">
  public static function time_quantum($bg_time,$end_time){
      date_default_timezone_set("Asia/Shanghai");
      $time = intval (date("Hi"));
      if ($time > str_replace(":","",$bg_time)  && $time < str_replace(":","",$end_time)) {
          return true;
      }else{
          return false;
      }
  }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="">
    /**
     *  @desc 根据两点间的经纬度计算距离
     *  @param float $lat1 纬度值
     *  @param float $lng1 经度值
     *  @param float $lat2 纬度值
     *  @param float $lng2 经度值
     */
    public static function getDistance($lat1, $lng1, $lat2, $lng2)
    {
        $earthRadius = 6367000; //approximate radius of earth in meters

        /*
          Convert these degrees to radians
          to work with the formula
        */

        $lat1 = ($lat1 * pi() ) / 180;
        $lng1 = ($lng1 * pi() ) / 180;

        $lat2 = ($lat2 * pi() ) / 180;
        $lng2 = ($lng2 * pi() ) / 180;

        /*
          Using the
          Haversine formula

          http://en.wikipedia.org/wiki/Haversine_formula

          calculate the distance
        */

        $calcLongitude = $lng2 - $lng1;
        $calcLatitude = $lat2 - $lat1;
        $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);  $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
        $calculatedDistance = $earthRadius * $stepTwo;

        return round($calculatedDistance/1000,2);
    }
// </editor-fold>
}
